<template>
  <div class="app-container">
    <div class="filter-container">
      <el-form ref="form" :model="form" :rules="rules" label-width="120px">
        <el-form-item label="门诊logo">
          <el-upload
            ref="upload"
            class="avatar-uploader"
            :action="action"
            :drag="true"
            :show-file-list="false"
            :on-success="handleAvatarSuccess"
            :before-upload="beforeAvatarUpload"
          >
            <img v-if="form.logo" :src="form.logo" class="avatar" />
            <i v-else class="el-icon-plus avatar-uploader-icon" />
          </el-upload>
        </el-form-item>

        <el-form-item label="门诊名称" prop="name">
          <el-input v-model="form.name" />
        </el-form-item>

        <el-form-item label="门诊地址" prop="addr">
          <el-input v-model="form.addr" />
        </el-form-item>

        <el-form-item label="门诊电话" prop="tel">
          <el-input v-model="form.tel" />
        </el-form-item>

        <el-form-item label="邮箱" prop="email">
          <el-input v-model="form.email" />
        </el-form-item>

        <el-form-item label="官网" prop="website">
          <el-input v-model="form.website" />
        </el-form-item>

        <el-form-item label="门诊简介">
          <el-input v-model="form.introduce" type="textarea" />
        </el-form-item>

        <el-form-item label="业务范围">
          <el-input v-model="form.business" type="textarea" />
        </el-form-item>

        <el-form-item>
          <el-button type="primary" @click="submitForm('form')">提交</el-button>
          <el-button @click="resetForm('form')">重置</el-button>
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>

<script>
import {
  validateTelephone,
  validatePhoneNumber,
  validEmail,
  validateUrl
} from "@/utils/validate";
import { clinicInfo, eidtClinic } from "@/api/clinic";

export default {
  data() {
    const isNUll = (rule, value, callback) => {
      if (!value) {
        return callback(new Error("不能为空"));
      } else {
        callback();
      }
    };
    const checkTel = (rule, value, callback) => {
      if (!validateTelephone(value) && !validatePhoneNumber(value)) {
        return callback(new Error("门诊电话格式不正确"));
      } else {
        callback();
      }
    };
    const checkMail = (rule, value, callback) => {
      if (value) {
        if (!validEmail(value)) {
          callback(new Error("邮箱格式不正确"));
        } else {
          callback();
        }
      } else {
        callback();
      }
    };
    const checkWebsite = (rule, value, callback) => {
      if (value) {
        if (!validateUrl(value)) {
          callback(new Error("网址格式不正确"));
        } else {
          callback();
        }
      } else {
        callback();
      }
    };
    return {
      action: "",

      form: {
        logo: "",
        name: "元康互联网科技有限公司",
        addr: "",
        tel: "",
        introduce: "",
        business: "",
        pic: [],
        email: "",
        website: "",
        version: 0
      },
      rules: {
        name: [
          { required: true, message: "请输入门诊名称", trigger: "blur" },
          { min: 2, max: 32, message: "长度在 2 到 32 个字符", trigger: "blur" }
        ],
        addr: [
          { required: true, message: "请输入门诊地址", trigger: "blur" },
          { min: 2, max: 32, message: "长度在 2 到 32 个字符", trigger: "blur" }
        ],
        tel: [
          {
            validator: isNUll,
            required: true,
            message: "请输入门诊电话",
            trigger: "blur"
          },
          {
            validator: checkTel,
            required: true,
            message: "门诊电话有误",
            trigger: "change"
          }
        ],
        email: [
          {
            validator: checkMail,
            required: false,
            message: "邮箱格式有误",
            trigger: "change"
          }
        ],
        website: [
          {
            validator: checkWebsite,
            required: false,
            message: "网址格式有误",
            trigger: "change"
          }
        ]
      }
    };
  },
  created() {
    this.action = process.env.VUE_APP_BASE_API + "/file/upload";
    this.getClinicInfo();
  },
  methods: {
    getClinicInfo() {
      clinicInfo().then(response => {
        if (response.code === 200) {
          this.form = response.data;
        }
      });
    },
    submitForm(formName) {
      const _that = this;
      this.$refs[formName].validate(valid => {
        if (valid) {
          const data = _that.form;
          data.pic = "";
          console.log(_that.form);
          eidtClinic(data).then(response => {
            if (response.code === 200) {
              this.$message({
                showClose: true,
                message: "编辑门诊信息成功息",
                type: "success"
              });
              this.form = response.data;
            }
          });
        } else {
          console.log("error submit!!");
          return false;
        }
      });
    },
    resetForm(formName) {
      this.$refs[formName].resetFields();
    },
    handleAvatarSuccess(res, f) {
      //this.form.imageUrl = URL.createObjectURL(f.raw)
      this.form.imageUrl = res.file;
      this.form.logo = res.file;
    },
    beforeAvatarUpload(file) {
      // 关键点注入用户凭证
      this.$refs.upload.headers.Authorization = this.$store.getters.token;
      console.log(this.$refs.upload.headers);
      const isJPG = file.type === "image/jpeg";
      const isPng = file.type === "image/png";
      const isLt2M = file.size / 1024 / 1024 < 2;

      if (!isJPG && !isPng) {
        this.$message.error("上传头像图片只能是 JPG 或 png 格式!");
      }
      if (!isLt2M) {
        this.$message.error("上传头像图片大小不能超过 2MB!");
      }
      return (isJPG || isPng) && isLt2M;
    }
  }
};
</script>

<style>
.avatar-uploader .el-upload {
  border: 1px dashed #d9d9d9;
  border-radius: 6px;
  cursor: pointer;
  position: relative;
  overflow: hidden;
}

.avatar-uploader .el-upload:hover {
  border-color: #409eff;
}

.avatar-uploader-icon {
  font-size: 28px;
  color: #8c939d;
  width: 178px;
  height: 178px;
  line-height: 178px;
  text-align: center;
}

.avatar {
  width: 178px;
  height: 178px;
  display: block;
}
</style>
